
package entity;

import ai_game.AIGame;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.*;
import physics.CollisionClass;
import sprites.EntityAnimationInstance;

/**
 *
 * @author ant
 */
public class Player extends GameObject
{
    private static final float speed = 5;
    
    private int animationDir;
    
    //Radar stuff for a1
    List<GameObject> radarResult = new ArrayList<>();
    final float radarRadius = 50;
    public Circle radarCircle = new Circle(pos.x,pos.y, radarRadius);
    double [] activationLevels = new double[4];
    
    
    //left center right, lines extend from pos to these vectors
    Vector2f[] feelers = new Vector2f[3];
    double[] feelersResults = {0,0,0};
    
    public Player(Vector2f pos)
    {
        this.pos = pos;
        collisionClass = CollisionClass.player;
        collisionShape = new Circle(pos.x-16, pos.y-16, 16);
    }
    
   
    
    @Override
    public void update()
    {
//        collisionShape.setX(pos.x);
//        collisionShape.setY(pos.y);
        
    }
   
    public void setDirection(Vector2f dir, boolean sethead){
        if(dir.x == 0 && dir.y == 0)
        {
            animation.stopAnmation();
            vel = dir;
        }
        else
        {
            if(sethead)headingRadians =Math.toRadians(dir.getTheta());
            int newAnimationDir = EntityAnimationInstance.getDir(Math.toRadians(dir.getTheta()));
            vel = dir.normalise().scale(speed);

            if(newAnimationDir != animationDir)
            {
                animation.setDir(newAnimationDir);
                animationDir = newAnimationDir;
            }
            
        }
    }
    
    public void setDirection(Vector2f dir)
    {
        setDirection(dir, true);
    }
    
    
    //assignment one requires a controllable agent to sense--to show that it works...
    public void sense(ArrayList<GameObject> gameObjects){
        
        feelersResults = feelWalls(Math.PI/6, 75);
        //System.out.println("rad: "+(float)headingRadians);
        activationLevels = AIGame.game.physicsEngine.pieSliceQuadSensor(pos, radarRadius, (float) headingRadians, CollisionClass.enemy, true);
        //Adjacent Enemies Sensing
        radarResult = AIGame.game.physicsEngine.getWithinRadius(pos, radarRadius, CollisionClass.enemy);
        
    }
    
    
    public void a1render(Rectangle camera, Graphics g){
        radarCircle = new Circle(pos.x,pos.y, radarRadius);

        DecimalFormat twoDec = new DecimalFormat("0.00");
        g.drawString("feelers: ["+twoDec.format(feelersResults[0])+", "+twoDec.format(feelersResults[1])+", "+twoDec.format(feelersResults[2])+"]", 365,10);
        
        String feelers = String.format("        %f\n%f        %f\n        %f", activationLevels[0],
                                                     activationLevels[1],
                                                     activationLevels[2],
                                                     activationLevels[3]);
        
        g.drawString(feelers, 20, 300);
        g.drawString("playerheading: "+headingRadians,10, 450);
        ///////////////////////////
        //Adjacent Enemies Sensor//
        ///////////////////////////
        
        //the radar circle
        g.pushTransform();
        g.translate(-camera.getMinX(), -camera.getMinY());
        g.draw(radarCircle);
        g.popTransform();
        
        //draw text [(distance, heading)(distance, heading)]
        int i=80;
        g.drawString("Radar(dist,head)", 10, i);
        for(GameObject go : radarResult){
            if(go.getClass()==Enemy1.class){
                i+=10;  
                g.drawString("("+dist(go.pos,pos)+","+dir(pos,go.pos)+")", 10, i);
            }
        }
     }
        
     private double dist(Vector2f a, Vector2f b){
        double x = a.x - b.x;
        double y = a.y - b.y;
        return Math.sqrt(x*x+y*y);
     }
        
     
    
    public double rayCast(ArrayList<GameObject> gameObjects, Vector2f pos, Vector2f feeler) {
        
        GeomUtil geomUtil = new GeomUtil();
        
        //float dist_x = (float) Math.cos(angleRadians);
        //float dist_y = (float) Math.sin(angleRadians);

        //Line segment = new Line(pos.x, pos.y, pos.x+dist_x, pos.y + dist_y);
        Line segment = new Line(pos, feeler);
        
        
        
        GameObject closest = null;
        //double distSq = Double.MAX_VALUE;
          double distSq=dist(pos,feeler);  
        for(GameObject obj : gameObjects)
        {
            if(this == obj || obj.collisionClass == CollisionClass.notCollidable)
                continue;
            
            GeomUtil.HitResult result = geomUtil.intersect(obj.collisionShape,segment);
            
            
            if(result != null)
            {
                //double crntDistSq = (pos.x - result.pt.x)*(pos.x - result.pt.x) +
                //                    (pos.y - result.pt.y)*(pos.y - result.pt.y);
                
                //if(crntDistSq < distSq)
                //    closest = obj;
                
                double d = dist(pos,result.pt);
                if(d<distSq) distSq=d;
            }
            
        }
        
        return distSq;
    }

    private double dir(Vector2f a, Vector2f b) {
        double retval = Math.atan2(a.x-b.x,a.y-b.y);
        retval = retval+Math.PI/2;
        if(retval<0) retval += 2*Math.PI;
        return retval;
        
    }
}
